package com.fishcoco.app.yunhang.application;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.StringUtils;
import com.fishcoco.app.yunhang.BuildConfig;
import com.fishcoco.app.yunhang.utils.netty.meibeike.meiphoto.common.clientconnect.ClientConnectFactory;
import com.tencent.bugly.Bugly;

import org.greenrobot.eventbus.EventBus;

import static com.fishcoco.app.yunhang.utils.constant.ConstantUtil.SP_KEY_APPNAME;
import static com.fishcoco.app.yunhang.utils.constant.ConstantUtil.SP_KEY_MACADDR;

/**
 * Created by app on 2017/7/18.
 */

public class YHApp extends UtilsApp {

    @Override
    public void onCreate() {
        super.onCreate();
        Bugly.init(getApplicationContext(), "3c5a088244", BuildConfig.DEBUG);
        // init EventBus Index
        EventBus.builder()
//                .addIndex(new MyEventBusIndex())
                .logNoSubscriberMessages(false)
                .installDefaultEventBus();

        String macAddr = SPUtils.getInstance(SP_KEY_APPNAME).getString(SP_KEY_MACADDR);
        if (StringUtils.isTrimEmpty(macAddr))
        {
            macAddr = getMacAddress();
            SPUtils.getInstance(SP_KEY_APPNAME).put(SP_KEY_MACADDR,macAddr);
        }
        ClientConnectFactory.getInstance().init(this);
    }

    public String getMacAddress() {

        String macAddress =null;
        WifiManager wifiManager =
                (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = (null== wifiManager ?null: wifiManager.getConnectionInfo());

        if(!wifiManager.isWifiEnabled())
        {
            //必须先打开，才能获取到MAC地址
            wifiManager.setWifiEnabled(true);
            wifiManager.setWifiEnabled(false);
        }
        if(null!= info) {
            macAddress = DeviceUtils.getMacAddress();
        }
        return macAddress;
    }
}
